import 'dart:io';

import 'package:automated_test/common/db_helper.dart';
import 'package:automated_test/routers/login/auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:permission_handler/permission_handler.dart';

import 'common/network.dart';
import 'l10n/DemoLocalizations.dart';
import 'routers/deviceDebug/gate_test.dart';
import 'routers/login/login.dart';
import 'routers/qr/qrscan.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  ///申请存储管理权限
  if (await Permission.storage.request().isGranted
      // && await Permission.manageExternalStorage.request().isGranted
      ) {
    ///网络请求初始化
    Network.init();

    ///数据库初始化
    await DbHelper.init();
    runApp(MyApp());
  } else {
    ///没有权限则退出
    exit(0);
  }
}

///配置EasyLoading属性
void configLoading() {
  EasyLoading.instance
    ..displayDuration = const Duration(milliseconds: 2000)
    ..indicatorType = EasyLoadingIndicatorType.wave
    ..loadingStyle = EasyLoadingStyle.dark
    ..indicatorSize = 45.0
    ..radius = 10.0
    ..progressColor = Colors.yellow
    ..backgroundColor = Colors.green
    ..indicatorColor = Colors.yellow;
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: const LoginPage(),
      builder: EasyLoading.init(),
      localizationsDelegates: const [
        DemoLocalizationsDelegate(),
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale("en"),
        Locale("zh"),
      ],
      routes: {
        "/QRScan": (context) => QRScanPage(),
        "/GateTest": (context) => GateTest(),
        "/Auth": (context) => Auth(),
      },
    );
  }
}
